﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQData;
using System.Data.Linq;
using System.Data;
using VQWEB.common;

namespace VQWEB.administrator.sources
{
    public class EnglishData
    {
        public static IList<object> ViewClassroom() {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_classroom>(
                null,
                null,
                null,
                null,
                c => new
                {
                    id = c.classroom_id,
                    title = c.classroom_title,
                    content = c.classroom_content,
                    active = c.classroom_active
                }
                );

            return list;
        }
        public EnglishData() { }

        #region Courses
        public int InsertOrUpdateCourses( eng_course obj)
        {
            
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.course_id > 0)
                    {
                        eng_course flag = GetCourseById(obj.course_id);                        
                        if (flag != null)
                        {
                            context.eng_courses.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.course_id;
                        }
                    }
                    else
                    {
                        context.eng_courses.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.course_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }          
            
        }
        public eng_course GetCourseById(int Id) {
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_courses.Where(c => c.course_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return new eng_course();
            }
        }
        public DataTable ViewCourse() {
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_courses
                            select new { 
                                course_id = c.course_id,
                                course_code=c.course_code,
                                course_title = c.course_title,
                                level = c.eng_lectures_levels.Count,
                                test =c.eng_tests.Count,
                                course_active = c.course_active
                            };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }
        public void DeleteCourses(List<int> array_id) {
        
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_courses
                            where array_id.Contains(c.course_id)
                            select c;
                if (query.Any())
                {
                    context.eng_courses.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        }
        public void LockUnLockCourses(List<int> array_id) {

            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_courses
                            where array_id.Contains(c.course_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.course_active = (item.course_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        }
        #endregion

        #region Lectures
        public eng_lecture GetLecutureById(int Id) {
           using (EnglishModelDataContext content = new EnglishModelDataContext()) {
               var query = content.eng_lectures.Where(c => c.lecture_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return null;
            }
        }

        public DataTable ViewLectures() { 
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                 var query = from c in context.eng_lectures
                             select new { 
                                lecture_id = c.lecture_id,
                                type_code = c.eng_lectures_chapter.eng_lectures_table.type_code,
                                chapter_lecture_id = c.chapter_lecture_id,
                                chapter_lecture_title = c.eng_lectures_chapter.chapter_lecture_title,                        
                                lecture_title = c.lecture_title,
                                lecture_content_english = c.lecture_content_english,
                                lecture_content_vietnam = c.lecture_content_vietnam,
                                lecture_content_note = c.lecture_content_note,
                                excercise = c.eng_lecture_excercises.Count,
                                lecture_active = c.lecture_active
                             
                             };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }

        public int InsertOrUpdateLectures(eng_lecture obj)
        {
            
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.lecture_id > 0)
                    {
                        var flag = GetLecutureById(obj.lecture_id);                        
                        if (flag != null)
                        {
                            context.eng_lectures.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.lecture_id;
                        }
                    }
                    else
                    {
                        context.eng_lectures.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.lecture_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }

        public void DeleteLectures(List<int> array_id) { 
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_lectures
                            where array_id.Contains(c.lecture_id)
                            select c;
                if (query.Any())
                {
                    context.eng_lectures.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        }

        public void LockUnlockLectures(List<int> array_id) {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_lectures
                            where array_id.Contains(c.lecture_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.lecture_active = (item.lecture_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        }
        #endregion

        #region Lecture_Level
        public eng_lectures_level GetLectureLevelById(int Id) {
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_lectures_levels.Where(c => c.level_lecture_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return null;
            }
        }
        public DataTable ViewLectureLevels() {
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                 var query = from c in context.eng_lectures_levels
                             orderby c.eng_course.course_title
                             select new { 
                                level_lecture_id = c.level_lecture_id,                               
                                course_id = c.course_id,
                                course_title = c.eng_course.course_title,
                                level_lecture_title = c.level_lecture_title,
                                level_lecture_content = c.level_lecture_content,
                                table = c.eng_lectures_tables.Count,
                                level_lecture_active = c.level_lecture_active
                             };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }
        public int InsertOrUpdateLectureLevels(eng_lectures_level obj) {
            
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.level_lecture_id > 0)
                    {
                        var flag = GetLectureLevelById(obj.level_lecture_id);                        
                        if (flag != null)
                        {
                            context.eng_lectures_levels.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.level_lecture_id;
                        }
                    }
                    else
                    {
                        context.eng_lectures_levels.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.level_lecture_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        
        }
        public void DeleteLectureLevels(List<int> array_id) {
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_lectures_levels
                            where array_id.Contains(c.level_lecture_id)
                            select c;
                if (query.Any())
                {
                    context.eng_lectures_levels.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        
        }
        public void LockUnLockLectureLevels(List<int> array_id) {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_lectures_levels
                            where array_id.Contains(c.level_lecture_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.level_lecture_active = (item.level_lecture_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }
        #endregion

        #region Lecture_Table
        public eng_lectures_table GetLectureTableById(int Id) { 
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_lectures_tables.Where(c => c.table_lecture_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return null;
            }
        
        }
        public DataTable ViewLectureTables() { 
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                 var query = from c in context.eng_lectures_tables
                             orderby c.eng_lectures_level.eng_course.course_title
                             select new { 
                                table_lecture_id = c.table_lecture_id,
                                course = c.eng_lectures_level.eng_course.course_title,
                                type_code = c.type_code,
                                level_lecture_title = from p in context.eng_lectures_levels
                                                      where p.level_lecture_id.Equals(c.level_lecture_id)
                                                      select p.level_lecture_title,                               
                                table_lecture_title = c.table_lecture_title,
                                table_lecture_content = c.table_lecture_content,
                                chapter = c.eng_lectures_chapters.Count,
                                table_lecture_active = c.table_lecture_active
                             };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }
        public int InsertOrUpdateLectureTables( eng_lectures_table obj) { 
             
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.table_lecture_id > 0)
                    {
                        var flag = GetLectureTableById(obj.table_lecture_id);                        
                        if (flag != null)
                        {
                            context.eng_lectures_tables.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.table_lecture_id;
                        }
                    }
                    else
                    {
                        context.eng_lectures_tables.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.table_lecture_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        
        }
        public void DeleteLectureTables(List<int> array_id) { 
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_lectures_tables
                            where array_id.Contains(c.table_lecture_id)
                            select c;
                if (query.Any())
                {
                    context.eng_lectures_tables.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        
        }
        public void LockUnLockLectureTables(List<int> array_id) {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_lectures_tables
                            where array_id.Contains(c.table_lecture_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.table_lecture_active = (item.table_lecture_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }
        #endregion

        #region Lecture_Chapter
        public eng_lectures_chapter GetLectureChapterById(int Id) {
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_lectures_chapters.Where(c => c.chapter_lecture_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return null;
            }
        
        }
        public DataTable ViewLectureChapters() { 
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                 var query = from c in context.eng_lectures_chapters
                             orderby c.eng_lectures_table.table_lecture_title
                             select new { 
                                chapter_lecture_id = c.chapter_lecture_id,
                                course = c.eng_lectures_table.eng_lectures_level.eng_course.course_title,
                                level = c.eng_lectures_table.eng_lectures_level.level_lecture_title,
                                type_code = c.eng_lectures_table.type_code,
                                table_lecture_title = from p in context.eng_lectures_tables
                                                      where p.table_lecture_id.Equals(c.table_lecture_id)
                                                      select p.table_lecture_title,
                                chapter_lecture_title = c.chapter_lecture_title,
                                chapter_lecture_content = c.chapter_lecture_content,
                                lecture = c.eng_lectures.Count,
                                chapter_lecture_active = c.chapter_lecture_active
                             };
                if (query.Any())
                {                    
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        }
        public int InsertOrUpdateLectureChapters(eng_lectures_chapter obj) {
            
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.chapter_lecture_id > 0)
                    {
                        var flag = GetLectureChapterById(obj.chapter_lecture_id);                        
                        if (flag != null)
                        {
                            context.eng_lectures_chapters.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.chapter_lecture_id;
                        }
                    }
                    else
                    {
                        context.eng_lectures_chapters.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.chapter_lecture_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        }
        public void DeleteLectureChapters(List<int> array_id) { 
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_lectures_chapters
                            where array_id.Contains(c.chapter_lecture_id)
                            select c;
                if (query.Any())
                {
                    context.eng_lectures_chapters.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        
        }
        public void LockUnLockLectureChapters(List<int> array_id) {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_lectures_chapters
                            where array_id.Contains(c.chapter_lecture_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.chapter_lecture_active = (item.chapter_lecture_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }
        #endregion

        #region Tests
        public eng_test GetTestById(int Id) { 
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_tests.Where(c => c.test_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return null;
            }
        
        }

        public DataTable ViewTests() { 
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                 var query = from c in context.eng_tests
                             orderby c.eng_course.course_title
                             select new
                             {
                                 test_id = c.test_id,
                                 course_id = c.course_id,
                                 course_title = c.eng_course.course_title,
                                 type_code = c.type_code,
                                 test_title = c.test_title,
                                 test_content = c.test_content,
                                 test_time = c.test_time,
                                 excercise = (from p in context.eng_tests_parts
                                             join e in context.eng_test_excercises on p.part_test_id equals e.part_test_id
                                             where c.test_id.Equals(p.test_id)
                                             select e.excercise_id).Count(),
                                 part_test = c.eng_tests_parts.Count,
                                 test_active = c.test_active

                             };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }

        public int InsertOrUpdateTests(eng_test obj) {
             
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.test_id > 0)
                    {
                        var flag = GetTestById(obj.test_id);                        
                        if (flag != null)
                        {
                            context.eng_tests.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.test_id;
                        }
                    }
                    else
                    {
                        context.eng_tests.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.test_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        
        }

        public void DeleteTests(List<int> array_id) {
        
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_tests
                            where array_id.Contains(c.test_id)
                            select c;
                if (query.Any())
                {
                    context.eng_tests.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        }

        public void LockUnLockTests(List<int> array_id) {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_tests
                            where array_id.Contains(c.test_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.test_active = (item.test_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }
        #endregion

        #region Test_Part
        public eng_tests_part GetTestPartById(int Id) {
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_tests_parts.Where(c => c.part_test_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return null;
            }
        }
        public DataTable ViewTestParts() { 
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                 var query = from c in context.eng_tests_parts
                             orderby c.eng_test.test_title
                             select new { 
                                part_test_id = c.part_test_id,
                                course = c.eng_test.eng_course.course_title,
                                type_code = c.eng_test.type_code,
                                test_title = c.eng_test.test_title,
                                part_test_title = c.part_test_title,
                                part_test_content = c.part_test_content,
                                part_test_file = c.part_test_file,
                                excercise = c.eng_test_excercises.Count,
                                part_test_active = c.part_test_active
                             };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        }
        public int InsertOrUpdateTestParts(eng_tests_part obj) { 
             
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.part_test_id > 0)
                    {
                        var flag = GetTestPartById(obj.part_test_id);                        
                        if (flag != null)
                        {
                            context.eng_tests_parts.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.part_test_id;
                        }
                    }
                    else
                    {
                        context.eng_tests_parts.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.part_test_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        
        }
        public void DeleteTestParts(List<int> array_id) { 
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_tests_parts
                            where array_id.Contains(c.part_test_id)
                            select c;
                if (query.Any())
                {
                    context.eng_tests_parts.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        
        }
        public void LockUnLockTestParts(List<int> array_id) { 
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_tests_parts
                            where array_id.Contains(c.part_test_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.part_test_active = (item.part_test_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }

        #endregion

        #region Practices
        public eng_practice GetPracticeById(int Id) {
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_practices.Where(c => c.practice_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return null;
            }
        
        }
        public DataTable ViewPractices() { 
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                 var query = from c in context.eng_practices
                             select new { 
                                practice_id = c.practice_id,
                                course = c.eng_lectures_level.eng_course.course_title,                                
                                level_lecture_title = c.eng_lectures_level.level_lecture_title,
                                type_code = c.type_code,
                                part = c.eng_practices_parts.Count,
                                practice_title = c.practice_title,
                                practice_active = c.practice_active
                             };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        }

        public int InsertOrUpdatePractices(eng_practice obj) { 
             
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.practice_id > 0)
                    {
                        var flag = GetPracticeById(obj.practice_id);                        
                        if (flag != null)
                        {
                            context.eng_practices.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.practice_id;
                        }
                    }
                    else
                    {
                        context.eng_practices.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.practice_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        
        }
        public void DeletePractices(List<int> array_id) {
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_practices
                            where array_id.Contains(c.practice_id)
                            select c;
                if (query.Any())
                {
                    context.eng_practices.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        
        }

        public void LockUnLockPractices(List<int> array_id) { 
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_practices
                            where array_id.Contains(c.practice_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.practice_active = (item.practice_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }
        #endregion

        #region Practice_Part
        public eng_practices_part GetParcticePartById(int Id) { 
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_practices_parts.Where(c => c.part_practice_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return null;
            }
        
        }
        public DataTable ViewPracticeParts() { 
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                 var query = from c in context.eng_practices_parts
                             select new { 
                                part_practice_id = c.part_practice_id,
                                course = c.eng_practice.eng_lectures_level.eng_course.course_title,
                                level = c.eng_practice.eng_lectures_level.level_lecture_title,
                                practice_title = c.eng_practice.practice_title,
                                part_practice_title = c.part_practice_title,
                                excercise = c.eng_part_excercises.Count,
                                part_practice_active = c.part_practice_active
                             };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }
        public int InsertOrUpdatePracticeParts(eng_practices_part obj) {
             
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.part_practice_id > 0)
                    {
                        var flag = GetParcticePartById(obj.part_practice_id);                        
                        if (flag != null)
                        {
                            context.eng_practices_parts.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.part_practice_id;
                        }
                    }
                    else
                    {
                        context.eng_practices_parts.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.part_practice_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        
        }
        public void DeletePracticeParts(List<int> array_id) {
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_practices_parts
                            where array_id.Contains(c.part_practice_id)
                            select c;
                if (query.Any())
                {
                    context.eng_practices_parts.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        
        }
        public void LockUnLockPracticeParts(List<int> array_id) {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_practices_parts
                            where array_id.Contains(c.part_practice_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.part_practice_active = (item.part_practice_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }
        #endregion

        #region Excercises
        public eng_excercise GetExcerciseById(int Id) {
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_excercises.Where(c => c.excercise_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return null;
            }
        
        }
        public DataTable ViewExcercises() { 
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                 var query = from c in context.eng_excercises
                             select new { 
                                excercise_id = c.excercise_id,  
                                type_code = c.type_code,
                                excercise_file = c.excercise_file,
                                excercise_title = c.excercise_title,
                                excercise_content = c.excercise_content,
                                excercise_answer = c.excercise_answer,
                                excercise_explain = c.excercise_explain,
                                excercise_active = c.excercise_active
                             
                             };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }

        public List<eng_excercise> ViewAllExcercise() { 
        
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from e in context.eng_excercises
                            select e;
                if (query.Any())
                    return query.ToList();
                else
                    return new List<eng_excercise>();
            }
        }

        public Dictionary<int, string> ViewAnswerExcercise(List<int> array_id) {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from e in context.eng_excercises
                            where array_id.Contains(e.excercise_id)
                            select new { 
                                excercise_id = e.excercise_id,
                                excercise_answer = e.excercise_answer.Trim()
                            };
                if (query.Any())
                    return query.ToDictionary(t => t.excercise_id, t => t.excercise_answer);
                else
                    return new Dictionary<int, string>();
            }
        }

        public int InsertOrUpdateExcercises(eng_excercise obj) { 
            
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.excercise_id > 0)
                    {
                        var flag = GetExcerciseById(obj.excercise_id);                        
                        if (flag != null)
                        {
                            context.eng_excercises.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.excercise_id;
                        }
                    }
                    else
                    {
                        context.eng_excercises.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.excercise_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        
        }
        public void DeleteExcercises(List<int> array_id) { 
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_excercises
                            where array_id.Contains(c.excercise_id)
                            select c;
                if (query.Any())
                {
                    context.eng_excercises.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        
        }
        public void LockUnLockExcercises(List<int> array_id) { 
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_excercises
                            where array_id.Contains(c.excercise_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.excercise_active = (item.excercise_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }


        #endregion

        #region Group
        public eng_excercises_group GetExcerciseGroupById(int Id) { 
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_excercises_groups.Where(c => c.group_excercise_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return null;
            }
        }
        public DataTable ViewExcerciseGroups() { 
             using(EnglishModelDataContext context = new EnglishModelDataContext()){
                 var query = from c in context.eng_excercises_groups
                             select new { 
                                group_excercise_id = c.group_excercise_id,                                
                                group_excercise_content_first = c.group_excercise_content_first,
                                group_excercise_content_last = c.group_excercise_content_last,
                                group_excercise_file = c.group_excercise_file,
                                group_excercise_title = c.group_excercise_title,
                                type_code=c.type_code,
                                excercise = c.eng_group_excercises.Count,
                                group_excercise_active = c.group_excercise_active
                             
                             };
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }
        public int InsertOrUpdateExcerciseGroups(eng_excercises_group obj) { 
             
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.group_excercise_id > 0)
                    {
                        var flag = GetExcerciseGroupById(obj.group_excercise_id);                        
                        if (flag != null)
                        {
                            context.eng_excercises_groups.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.group_excercise_id;
                        }
                    }
                    else
                    {
                        context.eng_excercises_groups.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.group_excercise_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            
        
        }
        public void DeleteExcerciseGroups(List<int> array_id) { 
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_excercises_groups
                            where array_id.Contains(c.group_excercise_id)
                            select c;
                if (query.Any())
                {
                    context.eng_excercises_groups.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        
        }
        public void LockUnLockExcerciseGroups(List<int> array_id) {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_excercises_groups
                            where array_id.Contains(c.group_excercise_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.group_excercise_active = (item.group_excercise_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }
        #endregion

        #region Group_Excercise
        public bool CheckExcerciseInGroup(int excercise_id, int group_id)
        {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                bool flag = false;
                var query = context.eng_group_excercises.Where(c => c.group_excercise_id.Equals(group_id) && c.excercise_id.Equals(excercise_id));
                if (query.Any())
                    flag = true;
                return flag;
            }
        }
        public string InsertOrUpdateGroup_Excercise(int[] excercise_id, int group_id)
        {
            try
            {
                string sResponse = string.Empty;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    var query = context.eng_group_excercises.Where(c => c.group_excercise_id.Equals(group_id));
                    if (query.Any())
                    {
                        context.eng_group_excercises.DeleteAllOnSubmit(query);
                        context.SubmitChanges();
                    } 
                    
                    if (excercise_id != null)
                    {
                        List<eng_group_excercise> colection = new List<eng_group_excercise>();
                        foreach (int item in excercise_id)
                        {
                            eng_group_excercise eng_group_excercise = new eng_group_excercise();
                            eng_group_excercise.excercise_id = item;
                            eng_group_excercise.group_excercise_id = group_id;
                            colection.Add(eng_group_excercise);
                           
                        }
                        context.eng_group_excercises.InsertAllOnSubmit(colection);
                        context.SubmitChanges();
                        sResponse = "InsertGroup_Excercise";
                    }
                }
                return sResponse;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region Part_Test_Group
        public bool CheckGroupInPartTest(int group_id, int part_test_id)
        {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                bool flag = false;
                var query = context.eng_test_part_groups.Where(c => c.group_excercise_id.Equals(group_id) && c.part_test_id.Equals(part_test_id));
                if (query.Any())
                    flag = true;
                return flag;
            }
        }
        public string InsertOrUpdatePartTest_Group(int[] group_id, int part_test_id)
        {
            try
            {
                string sResponse = string.Empty;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    var query = context.eng_test_part_groups.Where(c => c.part_test_id.Equals(part_test_id));
                    if (query.Any())
                    {
                        context.eng_test_part_groups.DeleteAllOnSubmit(query);
                        context.SubmitChanges();
                    } 
                    
                    if (group_id != null)
                    {
                        List<eng_test_part_group> colection = new List<eng_test_part_group>();
                        foreach (int item in group_id)
                        {
                            eng_test_part_group eng_test_part_group = new eng_test_part_group();
                            eng_test_part_group.group_excercise_id = item;
                            eng_test_part_group.part_test_id = part_test_id;
                            colection.Add(eng_test_part_group);
                           
                        }
                        context.eng_test_part_groups.InsertAllOnSubmit(colection);
                        context.SubmitChanges();
                        sResponse = "InsertPartTest_Group";
                    }
                }
                return sResponse;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }       
        #endregion

        #region Part_Test_Excercise
        public bool CheckExcerciseInPartTest(int excercise_id, int part_test_id)
        {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                bool flag = false;
                var query = context.eng_test_excercises.Where(c => c.part_test_id.Equals(part_test_id) && c.excercise_id.Equals(excercise_id));
                if (query.Any())
                    flag = true;
                return flag;
            }
        }
        public string InsertOrUpdatePartTest_Exercise(int[] excercise_id, int part_test_id)
        {
            try
            {
                string sResponse = string.Empty;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    var query = context.eng_test_excercises.Where(c => c.part_test_id.Equals(part_test_id));
                    if (query.Any())
                    {
                        context.eng_test_excercises.DeleteAllOnSubmit(query);
                        context.SubmitChanges();
                    }

                    if (excercise_id != null)
                    {
                        List<eng_test_excercise> colection = new List<eng_test_excercise>();
                        foreach (int item in excercise_id)
                        {
                            eng_test_excercise eng_test_excercise = new eng_test_excercise();
                            eng_test_excercise.excercise_id = item;
                            eng_test_excercise.part_test_id = part_test_id;
                            colection.Add(eng_test_excercise);

                        }
                        context.eng_test_excercises.InsertAllOnSubmit(colection);
                        context.SubmitChanges();
                        sResponse = "InsertPartTest_Excercise";
                    }
                }
                return sResponse;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region Lecture_Excercise
        public bool CheckExcerciseInLecture(int excercise_id, int lecture_id)
        {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                bool flag = false;
                var query = context.eng_lecture_excercises.Where(c => c.lecture_id.Equals(lecture_id) && c.excercise_id.Equals(excercise_id));
                if (query.Any())
                    flag = true;
                return flag;
            }
        }
        public string InsertOrUpdateLecture_Exercise(int[] excercise_id, int lecture_id)
        {
            try
            {
                string sResponse = string.Empty;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    var query = context.eng_lecture_excercises.Where(c => c.lecture_id.Equals(lecture_id));
                    if (query.Any())
                    {
                        context.eng_lecture_excercises.DeleteAllOnSubmit(query);
                        context.SubmitChanges();
                    }

                    if (excercise_id != null)
                    {
                        List<eng_lecture_excercise> colection = new List<eng_lecture_excercise>();
                        foreach (int item in excercise_id)
                        {
                            eng_lecture_excercise eng_lecture_excercises = new eng_lecture_excercise();
                            eng_lecture_excercises.excercise_id = item;
                            eng_lecture_excercises.lecture_id = lecture_id;
                            colection.Add(eng_lecture_excercises);

                        }
                        context.eng_lecture_excercises.InsertAllOnSubmit(colection);
                        context.SubmitChanges();
                        sResponse = "InsertLecture_Excercise";
                    }
                }
                return sResponse;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region Practice_Part_Excercise
        public bool CheckExcerciseInPracticePart(int excercise_id, int part_practice_id)
        {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                bool flag = false;
                var query = context.eng_part_excercises.Where(c => c.part_practice_id.Equals(part_practice_id) && c.excercise_id.Equals(excercise_id));
                if (query.Any())
                    flag = true;
                return flag;
            }
        }
        public string InsertOrUpdatePartPractice_Exercise(int[] excercise_id, int part_practice_id)
        {
            try
            {
                string sResponse = string.Empty;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    var query = context.eng_part_excercises.Where(c => c.part_practice_id.Equals(part_practice_id));
                    if (query.Any())
                    {
                        context.eng_part_excercises.DeleteAllOnSubmit(query);
                        context.SubmitChanges();
                    }

                    if (excercise_id != null)
                    {
                        List<eng_part_excercise> colection = new List<eng_part_excercise>();
                        foreach (int item in excercise_id)
                        {
                            eng_part_excercise eng_part_excercises = new eng_part_excercise();
                            eng_part_excercises.excercise_id = item;
                            eng_part_excercises.part_practice_id = part_practice_id;
                            colection.Add(eng_part_excercises);

                        }
                        context.eng_part_excercises.InsertAllOnSubmit(colection);
                        context.SubmitChanges();
                        sResponse = "InsertPartPractice_Excercise";
                    }
                }
                return sResponse;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region Class Room
        public eng_classroom GetClassRoomById(int Id) {
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_classrooms.Where(c => c.classroom_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return new eng_classroom();
            }
        
        }
        public int InsertOrUpdateClassRoom(eng_classroom obj){
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.classroom_id > 0)
                    {
                        eng_classroom flag = GetClassRoomById(obj.classroom_id);                        
                        if (flag != null)
                        {
                            context.eng_classrooms.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.classroom_id;
                        }
                    }
                    else
                    {
                        context.eng_classrooms.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.classroom_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }  
        }
        public DataTable ViewClassRoom() { 
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_classrooms
                            select c;
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }
        public void DeleteClassRoom(List<int> array_id) { 
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_classrooms
                            where array_id.Contains(c.classroom_id)
                            select c;
                if (query.Any())
                {
                    context.eng_classrooms.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        }
        public void LockUnLockClassRoom(List<int> array_id) { 
        
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_classrooms
                            where array_id.Contains(c.classroom_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.classroom_active = (item.classroom_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        }
        #endregion

        #region Register
        public eng_register GetRegisterById(int Id) {
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_registers.Where(c => c.register_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return new eng_register();
            }
        
        }
        public int InsertOrUpdateRegister(eng_register obj) {
        
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.register_id > 0)
                    {
                        eng_register flag = GetRegisterById(obj.register_id);                        
                        if (flag != null)
                        {
                            context.eng_registers.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.register_id;
                        }
                    }
                    else
                    {
                        context.eng_registers.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.register_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }  
        }
        public DataTable ViewRegister() { 
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_registers
                            select c;
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }
        public void DeleteRegister(List<int> array_id) {
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_registers
                            where array_id.Contains(c.register_id)
                            select c;
                if (query.Any())
                {
                    context.eng_registers.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        
        }
        public void LockUnLockRegister(List<int> array_id) { 
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_registers
                            where array_id.Contains(c.register_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.register_active = (item.register_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }
        #endregion

        #region Table Time
        public eng_tabletime GetTableTimeById(int Id) {
            using (EnglishModelDataContext content = new EnglishModelDataContext()) {
                var query = content.eng_tabletimes.Where(c => c.tabletime_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return new eng_tabletime();
            }
        
        }
        public int InsertOrUpdateTableTime(eng_tabletime obj) { 
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.tabletime_id > 0)
                    {
                        eng_tabletime flag = GetTableTimeById(obj.tabletime_id);                        
                        if (flag != null)
                        {
                            context.eng_tabletimes.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.tabletime_id;
                        }
                    }
                    else
                    {
                        context.eng_tabletimes.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.tabletime_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }  
        
        }
        public DataTable ViewTableTime() {
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_tabletimes
                            select c;
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }
        
        }
        public void DeleteTableTime(List<int> array_id) { 
            using(EnglishModelDataContext context = new EnglishModelDataContext()){
                var query = from c in context.eng_tabletimes
                            where array_id.Contains(c.tabletime_id)
                            select c;
                if (query.Any())
                {
                    context.eng_tabletimes.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
                
            }
        
        }
        public void LockUnLockTableTime(List<int> array_id) {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_tabletimes
                            where array_id.Contains(c.tabletime_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.tabletime_active = (item.tabletime_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        
        }
        #endregion

        #region Schedule
        public eng_schedule GetScheduleById(int Id)
        {
            using (EnglishModelDataContext content = new EnglishModelDataContext())
            {
                var query = content.eng_schedules.Where(c => c.schedule_id.Equals(Id)).SingleOrDefault();
                if (query != null)
                    return query;
                else
                    return new eng_schedule();
            }

        }
        public int InsertOrUpdateSchedule(eng_schedule obj)
        {
            try
            {
                int Id = 0;
                using (EnglishModelDataContext context = new EnglishModelDataContext())
                {
                    if (obj.schedule_id > 0)
                    {
                        eng_schedule flag = GetScheduleById(obj.schedule_id);
                        if (flag != null)
                        {
                            context.eng_schedules.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.schedule_id;
                        }
                    }
                    else
                    {
                        context.eng_schedules.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.schedule_id;

                    }

                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }

        }
        public DataTable ViewSchedule()
        {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_schedules
                            select c;
                if (query.Any())
                {
                    return HelperData.LINQToDataTable(query);
                }
                else
                {
                    return new DataTable();
                }
            }

        }
        public void DeleteSchedule(List<int> array_id)
        {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_schedules
                            where array_id.Contains(c.schedule_id)
                            select c;
                if (query.Any())
                {
                    context.eng_schedules.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }

            }

        }
        public void LockUnLockSchedule(List<int> array_id)
        {
            using (EnglishModelDataContext context = new EnglishModelDataContext())
            {
                var query = from c in context.eng_schedules
                            where array_id.Contains(c.schedule_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.schedule_active = (item.schedule_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }

        }
        #endregion


    }
}